﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Waker.Common;

namespace Waker.Devices
{
    public partial class FormSafe : Base.FormBaseToolBar
    {

        public FormSafe()
        {
            InitializeComponent();
            this.Load += new EventHandler(FormSafe_Load);
            this.gridView1.RowClick+=new DevExpress.XtraGrid.Views.Grid.RowClickEventHandler(gridView1_RowClick);
        }
        private void FormSafe_Load(object sender, EventArgs e)
        {
            FormRefresh();
        }
        public override void FormRefresh()
        {
            var dt = new BAL.BAL_Device().GetAllSafeTimePeriod();
            this.gridControl1.DataSource = dt;
            if (this.gridView1.GroupCount > 0)
                gridView1.ExpandAllGroups();
        }
        protected override void FormAdd()
        {
            var f = new FormSafeEdit(0);
            var res = f.ShowDialog();
            if (res == DialogResult.OK)
                FormRefresh();
        }
        protected override void FormEdit()
        {
            if (gridView1.FocusedRowHandle < 0)
                return;
            var dr = gridView1.GetFocusedDataRow();
            if (dr == null)
                return;
            var f = new FormSafeEdit(Convert.ToInt32(dr["Safe_ID"]));
            var res = f.ShowDialog();
            if (res == DialogResult.OK)
                FormRefresh();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (e.Clicks > 1)
            {
                FormEdit();
            }
        }
        protected override void FormDelete()
        {
            if (gridView1.FocusedRowHandle < 0)
                return;
            var dr = gridView1.GetFocusedDataRow();
            if (dr == null || dr["Safe_ID"] == DBNull.Value)
                return;
            if (UI.ShowWarningOKCancel(string.Format("确定删除时段[{0}]吗?", dr["SafeName"]), "删除提示") != DialogResult.OK)
                return;
            var res = new BAL.BAL_Device().DeleteSafeTimePeriod(Convert.ToInt32(dr["Safe_ID"]));
            if (res)
            {
                UI.ShowInfo("删除成功");
                FormRefresh();
            }
            else
            {
                UI.ShowError("删除失败");
            }
        }
    }
}
